<template>
  <div class="app-container">

    <!-- 查询和其他操作 -->
    <div class="filter-container">
      <el-input v-model="listQuery.goodsName" clearable class="filter-item" style="width: 200px;" placeholder="请输入商品名称"/>
      <el-select v-model="listQuery.categoryId" placeholder="请选择" class="filter-item" clearable>
        <el-option
          v-for="item in categoryList"
          :key="item.categoryId"
          :label="item.category"
          :value="item.categoryId"
        />
      </el-select>
      <el-button class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">查找</el-button>
      <el-button class="filter-item" type="primary" icon="el-icon-edit" @click="handleCreate">添加</el-button>
      <el-button :disabled="multipleSelection.length===0" class="filter-item" type="danger" icon="el-icon-delete" @click="handleDeleteAll">批量删除</el-button>
    </div>

    <!-- 查询结果 -->
    <el-table v-loading="listLoading" :data="list" element-loading-text="正在查询中。。。" border fit highlight-current-row @selection-change="handleSelectionChange">

      <el-table-column type="selection" width="55"/>
      <el-table-column align="center" label="商品ID" prop="id"/>

      <el-table-column align="center" min-width="100" label="商品名称" prop="goodsName"/>

      <el-table-column align="center" label="所属分类" prop="category"/>
      <el-table-column align="center" label="价格" prop="price"/>
      <el-table-column align="center" label="商品状态">
        <template slot-scope="scope">
          <el-badge :type="scope.row.publishStatus == 0?'danger':'success'" is-dot class="item">{{ scope.row.publishStatus == 0 ? '下架':'上架' }}</el-badge>
        </template>
      </el-table-column>
      <el-table-column align="center" label="是否推荐">
        <template slot-scope="scope">
          {{ scope.row.recommendStatus === 0 ? '不推荐':'推荐' }}
        </template>
      </el-table-column>
      <el-table-column align="center" label="销量" prop="sale"/>
      <el-table-column align="center" label="创建时间" prop="created"/>

      <el-table-column align="center" label="操作" width="300" class-name="small-padding fixed-width" fixed="right">
        <template slot-scope="scope">
          <el-button type="primary" size="mini" @click="lookDetail(scope.row,true)">详情</el-button>
          <el-button :disabled="scope.row.publishStatus===1" type="primary" size="mini" @click="handleUpdate(scope.row)">编辑</el-button>
          <el-button :disabled="scope.row.publishStatus===1" type="danger" size="mini" @click="handleDelete(scope.row.id)">删除</el-button>
          <el-button v-if="scope.row.publishStatus===0" type="info" size="mini" @click="handleUp(scope.row.id)">上架</el-button>
          <el-button v-else type="info" size="mini" @click="handleDown(scope.row.id)">下架</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.size" @pagination="getList" />

    <el-tooltip placement="top" content="返回顶部">
      <back-to-top :visibility-height="100" />
    </el-tooltip>

  </div>
</template>

<style>
  .table-expand {
    font-size: 0;
  }
  .table-expand label {
    width: 100px;
    color: #99a9bf;
  }
  .table-expand .el-form-item {
    margin-right: 0;
    margin-bottom: 0;
  }
  .gallery {
    width: 80px;
    margin-right: 10px;
  }
</style>

<script>
import { listGoods, deleteGoods, downGoods, upGoods } from '@/api/goods'
import { listCategory } from '@/api/category'
import BackToTop from '@/components/BackToTop'
import Pagination from '@/components/Pagination' // Secondary package based on el-pagination

export default {
  name: 'GoodsList',
  components: { BackToTop, Pagination },
  data() {
    return {
      list: [],
      total: 0,
      listLoading: true,
      listQuery: {
        index: 0,
        size: 10,
        categoryId: null,
        goodsName: undefined
      },
      categoryList: [],

      multipleSelection: [],
      goodsDetail: '',
      detailDialogVisible: false,
      downloadLoading: false
    }
  },
  created() {
    this.getCategory()
    this.getList()
  },
  methods: {
    getCategory() {
      listCategory({ index: 0, size: 100 })
        .then(response => {
          this.categoryList = response.data.data.content
        })
        .catch(() => {
          this.categoryList = []
        })
    },
    handleSelectionChange(val) {
      this.multipleSelection = val
    },
    getList(e) {
      if (e && e.page) {
        this.listQuery.index = e.page - 1
      }
      this.listLoading = true
      listGoods(this.listQuery).then(response => {
        this.list = response.data.data.content
        this.total = response.data.data.totalRecords
        this.listLoading = false
      }).catch(() => {
        this.list = []
        this.total = 0
        this.listLoading = false
      })
    },
    handleFilter() {
      this.listQuery.index = 0
      this.getList()
    },
    handleCreate() {
      this.$router.push({ path: '/goods/create' })
    },
    handleUpdate(row) {
      this.$router.push({ path: '/goods/edit', query: { id: row.id }})
    },
    lookDetail(row, idDetail) {
      this.$router.push({ path: '/goods/edit', query: { id: row.id, isDetail: true }})
    },
    showDetail(detail) {
      this.goodsDetail = detail
      this.detailDialogVisible = true
    },
    // 删除
    handleDelete(id) {
      this.$confirm('此操作将永久删除该商品, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        const params = {
          idList: [id]
        }
        deleteGoods(params).then(res => {
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
          this.getList()
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    handleDeleteAll() {
      const idList = []
      this.multipleSelection.forEach(item => {
        idList.push(item.id)
      })
      deleteGoods({ idList }).then(res => {
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
        this.getList()
      })
    },
    handleUp(id) {
      upGoods(id).then(res => {
        this.$message.success('上架成功')
        this.getList()
      }).catch((error) => {
        this.$message.error(error.data.respCode.message)
      })
    },
    handleDown(id) {
      downGoods(id).then(res => {
        this.$message.success('下架成功')
        this.getList()
      }).catch((error) => {
        this.$message.error(error.data.respCode.message)
      })
    }
    // handleDownload() {
    //   this.downloadLoading = true
    //   import('@/vendor/Export2Excel').then(excel => {
    //     const tHeader = ['商品ID', '商品编号', '名称', '专柜价格', '当前价格', '是否新品', '是否热品', '是否在售', '首页主图', '宣传图片列表', '商品介绍', '详细介绍', '商品图片', '商品单位', '关键字', '类目ID', '品牌商ID']
    //     const filterVal = ['id', 'goodsSn', 'name', 'counterPrice', 'retailPrice', 'isNew', 'isHot', 'isOnSale', 'listPicUrl', 'gallery', 'brief', 'detail', 'picUrl', 'goodsUnit', 'keywords', 'categoryId', 'brandId']
    //     excel.export_json_to_excel2(tHeader, this.list, filterVal, '商品信息')
    //     this.downloadLoading = false
    //   })
    // }
  }
}
</script>
